from dataclasses import dataclass
from enum import Enum
import numpy as np


class E_MapType(Enum):
    Blank = 0  # 空白
    Obstacle = 1  # 障碍物
    StartPoint = 2  # 起始点
    EndPoint = 3  # 终点


map1 = [
    # 0,1, 2, 3, 4, 5, 6, 7]
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 2, 0, 1, 0, 1, 0, 0],
    [0, 1, 0, 0, 1, 0, 3, 0],
    [0, 1, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]


class MapImpl:
    def __init__(self, map_data):
        self.init_map(map_data)

    def init_map(self, map_data):
        dt_map = np.dtype(
            [
                ("type", np.int8),
                ("value", np.int32),
                ("w", np.uint32),
                ("h", np.uint32),
            ]
        )
        hight = len(map_data)
        width = len(map_data[0])
        self.map = np.zeros((hight, width), dtype=dt_map)
        for h in range(hight):
            for w in range(width):
                self.map[h][w]["type"] = map_data[h][w]
                self.map[h][w]["w"] = w
                self.map[h][w]["h"] = h

        self.map_h = hight
        self.map_w = width
        print(self.map)

    def get_adjacent_map(self, pos, direction: str):
        h, w = pos
        if direction == "up":
            h = h - 1
            if h < 0:
                return None
        elif direction == "down":
            h = h + 1
            if h > self.map_h - 1:
                return None
        elif direction == "left":
            w = w - 1
            if w < 0:
                return None
        elif direction == "right":
            w = w + 1
            if w > self.map_w - 1:
                return None
        return self.map[h][w]

    def get_start_point(self):
        idx = np.where(self.map["type"] == E_MapType.StartPoint.value)
        return self.map[idx]

    def get_end_point(self):
        idx = np.where(self.map["type"] == E_MapType.EndPoint.value)
        return self.map[idx]

    def a_start_search(self, start=None, end=None):
        if not start:
            start = self.get_start_point()
        if not end:
            end = self.get_end_point()
        


if __name__ == "__main__":
    map = MapImpl(map1)
    ret = map.get_adjacent_map((0, 0), "up")
    print(ret)
    ret = map.get_adjacent_map((1, 1), "up")
    print(ret)
    start_map = map.get_start_point()
    end_map = map.get_end_point()
    print(start_map)
    print(end_map)
